package com.exchange.consumer;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exchange.entity.model.ReservationModel;
import com.exchange.entity.pojo.ReservationRecord;
import com.exchange.entity.pojo.User;
import com.exchange.service.ReservationRecordService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Author : RulerWood
 * @Data : 2024/4/28 - 20:51
 */
@Component
public class BackReservationConfig {

    @Autowired
    private ReservationRecordService reservationRecordService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 监控死信队列，如果有消息则证明该消息来自预约，并且该消息已经过期了，修改状态
     * @param message
     */
    @RabbitListener(queues = "map-dead-queue")
    public void reservation(Message message) {
        System.err.println("------死信队列的监听--------");
        System.out.println(message.getBody());
        ReservationModel reservationModel = JSON.parseObject(message.getBody(), ReservationModel.class);
        System.out.println(reservationModel);
        //此时到期，查看当前预约状态是否是交易成功，如果不是则证明该用户已经超时，则删除消息，
        QueryWrapper<ReservationRecord> reservationRecordQueryWrapper = new QueryWrapper<>();
        reservationRecordQueryWrapper.eq("uid", reservationModel.getUid());
        reservationRecordQueryWrapper.eq("status",2);
        ReservationRecord one = this.reservationRecordService.getOne(reservationRecordQueryWrapper);
        if (ObjectUtil.isEmpty(one)){
            //预约没有过期消息
            // 如果交易成功则不予理会
            System.out.println("该预约未过期---消费消息即可");
        }else {
            //证明确实存在一条预约还未被处理，并且已经过期.删除消息
            this.reservationRecordService.remove(reservationRecordQueryWrapper);
        }

    }

    //20秒执行一次，判断当前用户的预约是否即将过期
//    @Scheduled(cron = "*/5 * * * * *")
//    public void reservation() {
//        //如果查询当前redis中的值抛出了异常则代表过期
//        System.out.println("定时任务执行了");
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
//                .getRequest();
//        if(ObjectUtil.isEmpty(request)){
//            return;
//        }
//        String token = request.getHeader("token");
//        System.out.println("用户的token是-------------》" + token);
//
//        //根据token拿取用户的信息，判断当前用户的预约是否快要到期
//        String s = this.redisTemplate.opsForValue().get("user:login" + token);
//        User user = JSON.parseObject(s, User.class);
//        if (ObjectUtil.isEmpty(user)){
//            return;
//        }else {
//            //用户信息存在，查找预约信息
//            QueryWrapper<ReservationRecord> reservationRecordQueryWrapper = new QueryWrapper<>();
//            reservationRecordQueryWrapper.eq("status",2);
//            reservationRecordQueryWrapper.eq("uid",user.getId());
//            ReservationRecord one = this.reservationRecordService.getOne(reservationRecordQueryWrapper);
//            if (ObjectUtil.isEmpty(one)){
//                //证明没有预约信息，无所谓
//            }else {
//                //有预约信息，判断当前预约信息还剩多长时间结束，如果小于5分钟，返回提示
//            }
//        }
//    }


}
